﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DnPakExplorer {

	/// <summary>
	/// 技能面板，管理角色应用的技能及其效果
	/// </summary>
	public class SkillPanel {
		private List<Skill> mSkills = new List<Skill>();
		private SkillProperty[] mProperties = new SkillProperty[(int)PropertyID.ID_MAX];

		public event SkillItemChangedHandler OnSkillItemChanged;

		public void AddSkill(Skill skill) {
			mSkills.Add(skill);
			CalculateProperties();
			if (OnSkillItemChanged != null) {
				OnSkillItemChanged();
			}
		}

		public void RemoveSkill(Skill skill) {
			mSkills.Remove(skill);
			CalculateProperties();
			if (OnSkillItemChanged != null) {
				OnSkillItemChanged();
			}
		}

		public void RemoveSkill(int index) {
			mSkills.RemoveAt(index);
			CalculateProperties();
			if (OnSkillItemChanged != null) {
				OnSkillItemChanged();
			}
		}

		public int SkillCount {
			get { return mSkills.Count; }
		}

		public void CalculateProperties() {
			mProperties = new SkillProperty[(int)PropertyID.ID_MAX];
			foreach (Skill skill in mSkills) {
				foreach (SkillEffect effect in skill) {
					PropertyID propertyId = effect.GetTargetPropertyId();
					if (propertyId>=0 && propertyId<PropertyID.ID_MAX) {
						switch (effect.GetPropertyFlag()) {
						case SkillEffect.Value:
							mProperties[(int)propertyId].value += float.Parse(effect.EffectClassValue);
							break;

						case SkillEffect.Percent:
							mProperties[(int)propertyId].percentValue += float.Parse(effect.EffectClassValue);
							break;

						case SkillEffect.Rate:
							mProperties[(int)propertyId].rateValue += float.Parse(effect.EffectClassValue);
							break;
						}
					}
				}
			}
		}

		public SkillProperty GetProperty(PropertyID id) {
			return mProperties[(int)id];
		}

		public Skill this[int index]{
			get { return mSkills[index]; }
		}

		public delegate void SkillItemChangedHandler();
	}

	public struct SkillProperty {
		public PropertyID targetProperty;
		public float value;
		public float percentValue;
		public float rateValue;

		public override string ToString() {
			return string.Format("{0}:{1:F0},{2:P},{3:F2}", PropertyUtil.GetPropertyName((int)targetProperty), value, percentValue, rateValue);
		}
	}
}
